﻿@model DiscountListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.Promotions.Discounts").Text;
}

<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit">
            <div class="x_title">
                <div class="caption">
                    <i class="fa fa-tags"></i>
                    @T("Admin.Promotions.Discounts")
                </div>
                <div class="actions btn-group btn-group-devided">
                    <a href="@Url.Action("Create")" class="btn green"><i class="fa fa-plus"></i><span class="hidden-xs"> @T("Admin.Common.AddNew") </span></a>
                    <vc:admin-widget widget-zone="discount_list_buttons" additional-data="null" />
                </div>
            </div>
            <div class="x_content form">
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="form-group">
                            <admin-label asp-for="SearchDiscountName" />
                            <div class="col-md-4 col-sm-6">
                                <admin-input asp-for="SearchDiscountName" />
                            </div>
                        </div>
                        <div class="form-group">
                            <admin-label asp-for="SearchDiscountCouponCode" />
                            <div class="col-md-4 col-sm-6">
                                <admin-input asp-for="SearchDiscountCouponCode" />
                            </div>
                        </div>
                        <div class="form-group">
                            <admin-label asp-for="SearchDiscountTypeId" />
                            <div class="col-md-4 col-sm-6">
                                <admin-select asp-for="SearchDiscountTypeId" asp-items="Model.AvailableDiscountTypes" />
                            </div>
                        </div>
                    </div>
                    <div class="form-actions">
                        <div class="row">
                            <div class="col-md-offset-3 col-sm-offset-3 col-md-9 col-sm-9">
                                <button class="btn btn-success filter-submit" id="search-discounts">
                                    <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="x_content">
                <div id="discounts-grid"></div>
            </div>
        </div>
    </div>
</div>

<script>
        $(document).ready(function () {
            $("#discounts-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("List", "Discount"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(adminAreaSettings.DefaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [{
                    field: "Name",
                    title: "@T("Admin.Promotions.Discounts.Fields.Name")",
                    width: 350,
                    template: '<a class="k-link" href="Edit/#=Id#">#=kendo.htmlEncode(Name)#</a>',
                }, {
                    field: "DiscountTypeName",
                    title: "@T("Admin.Promotions.Discounts.Fields.DiscountType")",
                    width: 250
                },{
                    field: "DiscountAmount",
                    title: "@T("Admin.Promotions.Discounts.Fields.Discount")",
                    width: 200,
                    template: '# if(!CalculateByPlugin) { if(UsePercentage) {# #=DiscountPercentage# % #} else {#  #=DiscountAmount# #=PrimaryStoreCurrencyCode# #} } # '
                }, {
                    field: "StartDate",
                    title: "@T("Admin.Promotions.Discounts.Fields.StartDate")",
                    width: 200,
                    type: "date",
                    format: "{0:G}"
                }, {
                    field: "EndDate",
                    title: "@T("Admin.Promotions.Discounts.Fields.EndDate")",
                    width: 200,
                    type: "date",
                    format: "{0:G}"
                }, {
                    field: "TimesUsed",
                    title: "@T("Admin.Promotions.Discounts.Fields.TimesUsed")",
                    width: 200
                }, {
                    field: "IsEnabled",
                    title: "@T("Admin.Promotions.Discounts.Fields.IsEnabled")",
                    width: 100,
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    template: '# if(IsEnabled) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                }]
            });
        });
</script>
<script type="text/javascript">

        $(document).ready(function () {

            $('#search-discounts').click(function () {
                var grid = $('#discounts-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                //grid.dataSource.read(); we already loaded the grid above using "page" function
                return false;
            });
        });

        $("#@Html.FieldIdFor(model => model.SearchDiscountName)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#search-discounts").click();
                return false;
            }
        });

        $("#@Html.FieldIdFor(model => model.SearchDiscountCouponCode)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#search-discounts").click();
                return false;
            }
        });

        function additionalData() {
            var data = {
                SearchDiscountTypeId: $('#@Html.FieldIdFor(model => model.SearchDiscountTypeId)').val(),
                SearchDiscountCouponCode: $('#@Html.FieldIdFor(model => model.SearchDiscountCouponCode)').val(),
                SearchDiscountName: $('#@Html.FieldIdFor(model => model.SearchDiscountName)').val()
            };
            addAntiForgeryToken(data);
            return data;
        }

</script>
